/*
 * Copyright (C) 2009 Dimitrios C. Michalakos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.cs.util.db.ioc;

import com.cs.util.db.ResultSetMetaInfo;
import com.cs.util.db.ioc.IOCException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.sql.Clob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

/**
 * MySqlText is an IO controller for handling input and output of MySql TEXT columns.
 * @author dimitris@jmike.gr
 */
public class MySqlText extends MySqlChar implements IOController {

    public MySqlText(ResultSetMetaInfo rsmi, int columnIndex) throws SQLException {
        super(rsmi, columnIndex);
    }

    public MySqlText(ResultSetMetaInfo rsmi, String table, String column) throws SQLException {
        super(rsmi, table, column);
    }

    /**
     * Returns the value of the controller's column at the specified row as a String.
     * @param rs the resultset containing the current row position and the field's value.
     * @return
     */
    @Override
    public String getValue(ResultSet rs) {
        try {
            final Clob clob = rs.getClob(columnIndex);
            StringBuilder s = new StringBuilder();
            int buf = -1;
            try {
                Reader in = clob.getCharacterStream();
                while ((buf = in.read()) > -1) {
                    s.append((char) buf);
                }
                in.close();
            } catch (IOException ex) {
                /* do nothing */
            }
            return s.toString();
        } catch (SQLException ex) {
            /* do nothing */
        }
        return null;
    }

    /**
     * Set the value of the designated parameter in the specified PreparedStatement.
     * @param ps an object that represents a precompiled SQL statement.
     * @param parameterIndex the first parameter in the PreparedStatement is 1, the second is 2, ...
     * @param value the value of the parameter.
     * @throws java.sql.SQLException
     * @throws IOCException
     */
    @Override
    public void setParameterValue(PreparedStatement ps, int parameterIndex, String value) throws SQLException, IOCException {
        final String x = validate(value);
        if (x == null) {
            ps.setNull(parameterIndex, Types.CLOB);
        } else {
            ps.setClob(parameterIndex, new StringReader(x));
        }
    }

    /**
     * Returns the value of the controller's column as a URL encoded String.
     * Please note that this function will always return null because of URL length limitations.
     * @param rs the resultset containing the current row position and the field's value.
     * @return
     */
    @Override
    public String getURLEncodedValue(ResultSet rs) {
        return null;
    }

    /**
     * Returns the character size of the controller's column at the specified row as a String.
     * @param rs the resultset containing the current row position and the field's value.
     * @return
     */
    @Override
    public String getXhtmlTableData(ResultSet rs) {
        try {
            final long x = rs.getClob(columnIndex).length();
            return String.valueOf(x) + " chars";
        } catch (SQLException ex) {
            return "0 chars";
        }
    }

    /**
     * Returns a Xhtml form element for the controller's column.
     * @param rs the resultset containing the current row position and the field's value.
     * @param isEmpty a boolean flag that indicates whether this element contains data.
     * @return
     */
    @Override
    public String getXhtmlFormElement(ResultSet rs, boolean isEmpty) {
        StringBuilder s = new StringBuilder();
        s.append("<textarea");
        final String id = this.getId();
        s.append(" id=\"" + id + "\"");
        s.append(" name=\"" + id + "\"");
        if (locked) {//input values are locked
            s.append(" readonly=\"readonly\"");
        }
        s.append(">");
        final String value;
        if (locked) {//input values are locked
            value = this.lockValue;
        } else {//input values are not locked
            if (rs != null && !isEmpty) {//resultset is not null and contains at least one row
                value = this.getValue(rs);
            } else {//resultset is empty
                value = this.getDefaultValue();//get default value
            }
        }
        if (value != null) {//value is not null
            s.append(this.getValue(rs));
        }
        s.append("</textarea>");
        return s.toString();
    }
}
